package com.kaiwukj.android.ufamily.mvp.model;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.GsonUtils;
import com.kaiwukj.android.mcas.integration.IRepositoryManager;
import com.kaiwukj.android.mcas.mvp.BaseModel;
import com.kaiwukj.android.ufamily.mvp.http.api.service.MineService;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.MyAddressUpdateParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessDeviceResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessRecordResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyAddressResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseMemberResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.mine.MineContract;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;

public class MineModel extends BaseModel implements MineContract.Model {

    @Inject
    public MineModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public Observable<ListResp<MyAddressResult>> fetchMyAddress(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .fetchMyAddress(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> deleteAddress(int id) {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .deleteMyAddressById(id)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> updateAddress(MyAddressUpdateParams params) {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .updateMyAddressById(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> addAddress(MyAddressUpdateParams params) {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .addMyAddress(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<List<MyHouseResult>> fetchMyHouse() {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .fetchMyHouse()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<List<MyHouseMemberResult>> fetchHouseMember(Integer roomId) {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .fetchHouseMember(roomId)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> addHouseMember(Map<String, Object> params) {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .addHouseMember(getRequestBody(GsonUtils.toJson(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> delHouseMember(int ownerRoomId) {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .delHouseMember(ownerRoomId)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<DoorAccessDeviceResult> fetchDoorAccessResult() {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .fetchDoorAccessDevice()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<DoorAccessRecordResult>> fetchDoorRecord(String month, int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .fetchDoorOpenHistory(month, page, pageSize)
                .compose(RxUtils.handleResult());
    }
}
